﻿using System.ComponentModel.DataAnnotations;
using User.EF.Model;
using User.Global.Constvalues;
using User.Global.Contracts.Membership;
using User.Global.Contracts.Message;
using User.Global.Resources;
using User.Web.Common.WebAttributes;
using User.Web.Contracts.Model.Account;

namespace User.Web.Models.Account
{
    /// <summary>
    /// Das Aktuelle Model zum Ändern des Passworts eines Users, wenn dieser sein Passwort 
    /// Achtung leitet von ModelBase ab.
    /// </summary>
    public class RecoverySetNewPasswordModel : ModelBase, IRecoverySetNewPasswordModel
    {
        #region Member
        [Required]
        [DataType(DataType.Password)]
        [StringLength(100, ErrorMessageResourceType = typeof(ErrorMessages), ErrorMessageResourceName = "MinPasswordLength", MinimumLength = ConstLengthValues.MinPasswordLength)]
        [Display(ResourceType = typeof(InputFieldNames), Name = "NewPassword")]
        [HtmlAttributes(CssClass = "form-control", Required = true, Placeholder = "Neues Passwort")]
        public string Password { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(ResourceType = typeof(InputFieldNames), Name = "PasswordConfirm")]
        [Compare("Password", ErrorMessageResourceType = typeof(ErrorMessages), ErrorMessageResourceName = "PasswordsDoNotMatch")]
        [HtmlAttributes(CssClass = "form-control", Required = true, Placeholder = "Neues Passwort bestätigen")]
        public string ConfirmPassword { get; set; }

        public string Token { get; set; }
        #endregion

        #region Public Functions
        /// <summary>
        /// Ändern des Passworts für den aktuellen User, dessen Token übergeben wurde.
        /// </summary>
        /// <returns>TRUE->Erfolgreich geändert | FALSE->Fehler beim Ändern</returns>
        public virtual bool ChangePassword(IDiMembership<Member> membership)
        {
            //Das neue Passwort für den User mit dem übergebenen Token setzten.
            if (membership.ResetPassword(Token, Password))
            {
                AddMessage.Add(DefaultMessages.PasswordChangedSuccessFully, MessageTypes.ok);
                return true;
            }
            return false;
        }
        #endregion
    }
}
